package com.stox.manager.impl;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.stox.manager.ScripManager;
import com.stox.manager.WatchListManager;
import com.stox.model.Scrip;
import com.stox.model.Watchlist;
import com.stox.repository.spring.data.jpa.WatchListJpaRepository;

@Component
@Transactional
public class WatchListManagerImpl implements WatchListManager{

	@Autowired
	private WatchListJpaRepository watchListJpaRepository;
	
	@Autowired
	private ScripManager scripManager;
	
	@Override
	@Transactional
	public void rename(Long id, String newName) {
		watchListJpaRepository.rename(id, newName);
	}
	
	@Override
	@Transactional
	public void addToWatchList(List<Long> entryIds, Long watchListId) {
		//Root watchlist on UI doesn't really exist on server side, so can not add entries to it.
		if(null != entryIds && !entryIds.isEmpty() && null != watchListId && 0 < watchListId){
			//Find if there is any better/faster way to do this - should not need to fetch watchlist, scrips, entries
			Watchlist watchList = watchListJpaRepository.findOne(watchListId);
			for(Long entryId : entryIds){
				Scrip scrip = scripManager.findOne(entryId);
				watchList.getEntries().add(scrip);
			}
			watchListJpaRepository.save(watchList);
		}
	}
	
	@Override
	@Transactional
	public void moveEntry(Long id, Long oldWatchlistId, Long newWatchListId) {
		Watchlist watchList = watchListJpaRepository.findOne(oldWatchlistId);
		Scrip scrip = scripManager.findOne(id);
		watchList.getEntries().remove(scrip);
		watchListJpaRepository.save(watchList);
		
		watchList = watchListJpaRepository.findOne(newWatchListId);
		watchList.getEntries().add(scrip);
		watchListJpaRepository.save(watchList);
	}
	
	@Override
	@Transactional
	public Watchlist save(Watchlist entity) {
		return watchListJpaRepository.save(entity);
	}

	@Override
	@Transactional
	public void delete(Long id) {
		watchListJpaRepository.delete(id);
	}

	@Override
	@Transactional
	public void deleteEntry(Long entryId, Long watchListId) {
		Watchlist watchList = watchListJpaRepository.findOne(watchListId);
		Scrip scrip = scripManager.findOne(entryId);
		watchList.getEntries().remove(scrip);
		watchListJpaRepository.save(watchList);
	}

	@Override
	@Transactional(readOnly=true)
	public Set<Watchlist> findByParentId(Long id) {
		if(null == id) id = 0l;
		return watchListJpaRepository.findByParentId(id);
	}

	@Override
	@Transactional(readOnly=true)
	public Set<Scrip> findEntriesByWatchListId(Long id) {
		Set<Scrip> scrips = watchListJpaRepository.findOne(id).getEntries();
		scrips.size(); //Just access anything from PersistentBag - Lazy-loading issue
		return scrips;
	}

	@Override
	@Transactional
	public void deleteByParentId(Long parentId) {
		watchListJpaRepository.deleteByParentId(parentId);
	}

	@Override
	@Transactional
	public void deleteEntriesByParentId(Long parentId) {
		Watchlist watchList = watchListJpaRepository.findOne(parentId);
		watchList.getEntries().clear();
		watchListJpaRepository.save(watchList);
	}

	@Override
	@Transactional
	public void clear(Long id) {
		deleteByParentId(id);
		deleteEntriesByParentId(id);
	}

}
